package study.alex.avalon;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Created with IntelliJ IDEA.
 * User: Alex
 * Date: 01.12.13
 * Time: 22:35
 * To change this template use File | Settings | File Templates.
 */
public class clientPanel {
    private JPanel panel1;
    private JTabbedPane tabbedPane1;
    private JFormattedTextField keyword;
    private JButton buttonFind;
    private JButton deleteButton;
    private JButton addButton;
    private JButton editButton;
    private JButton nextButton;
    private JButton previousButton;
    private JButton exitButton;
    private JEditorPane definitionPane;
    private JCheckBox editCheckBox;
    private JLabel label;
    private JPanel mainTab;
    private JRadioButton fileSource;
    private JRadioButton databaseSource;
    private JFormattedTextField fileName;
    private JButton fileNameSelectButton;
    private JFormattedTextField hostName;
    private JFormattedTextField databaseName;
    private JPanel settingTab;
    private JFormattedTextField userName;
    private JFormattedTextField password;
    private JButton connectDBButton;
    private JButton disconnectDBButton;
    private JButton copyToDBButton;
    private JLabel statusLine;

    public clientPanel() {
        clientPanelInitListeners();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("clientPanel");
        frame.setContentPane(new clientPanel().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public void clientPanelInitListeners() {
        buttonFind.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        previousButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        editCheckBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        keyword.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        databaseSource.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        fileSource.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        hostName.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        databaseName.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        userName.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        password.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        connectDBButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        disconnectDBButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        fileName.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        fileNameSelectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        copyToDBButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        fileSource.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }


    public void setData(clientBean data) {
        definitionPane.setText(data.getDefinition());
        keyword.setText(data.getKeyword());
        hostName.setText(data.getHostName());
        userName.setText(data.getUserName());
        databaseName.setText(data.getDatabaseName());
        password.setText(data.getPassword());
        fileName.setText(data.getFileName());
    }

    public void getData(clientBean data) {
        data.setDefinition(definitionPane.getText());
        data.setKeyword(keyword.getText());
        data.setHostName(hostName.getText());
        data.setUserName(userName.getText());
        data.setDatabaseName(databaseName.getText());
        data.setPassword(password.getText());
        data.setFileName(fileName.getText());
    }

    public boolean isModified(clientBean data) {
        if (definitionPane.getText() != null ? !definitionPane.getText().equals(data.getDefinition()) : data.getDefinition() != null)
            return true;
        if (keyword.getText() != null ? !keyword.getText().equals(data.getKeyword()) : data.getKeyword() != null)
            return true;
        if (hostName.getText() != null ? !hostName.getText().equals(data.getHostName()) : data.getHostName() != null)
            return true;
        if (userName.getText() != null ? !userName.getText().equals(data.getUserName()) : data.getUserName() != null)
            return true;
        if (databaseName.getText() != null ? !databaseName.getText().equals(data.getDatabaseName()) : data.getDatabaseName() != null)
            return true;
        if (password.getText() != null ? !password.getText().equals(data.getPassword()) : data.getPassword() != null)
            return true;
        if (fileName.getText() != null ? !fileName.getText().equals(data.getFileName()) : data.getFileName() != null)
            return true;
        return false;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(2, 1, new Insets(5, 5, 5, 5), -1, -1));
        panel1.setMinimumSize(new Dimension(750, 400));
        panel1.setPreferredSize(new Dimension(750, 400));
        tabbedPane1 = new JTabbedPane();
        panel1.add(tabbedPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(200, 350), null, 0, false));
        mainTab = new JPanel();
        mainTab.setLayout(new GridLayoutManager(5, 3, new Insets(5, 5, 5, 5), -1, -1));
        tabbedPane1.addTab("Главная", mainTab);
        label = new JLabel();
        label.setText("Термин");
        mainTab.add(label, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonFind = new JButton();
        buttonFind.setActionCommand("findAction");
        buttonFind.setText("Искать");
        mainTab.add(buttonFind, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FormLayout("fill:d:grow", "center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        mainTab.add(panel2, new GridConstraints(1, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        addButton = new JButton();
        addButton.setActionCommand("addAction");
        addButton.setText("Добавить");
        CellConstraints cc = new CellConstraints();
        panel2.add(addButton, cc.xy(1, 1));
        editButton = new JButton();
        editButton.setActionCommand("editAction");
        editButton.setText("Редактировать");
        panel2.add(editButton, cc.xy(1, 3));
        deleteButton = new JButton();
        deleteButton.setActionCommand("deleteAction");
        deleteButton.setText("Удалить");
        panel2.add(deleteButton, cc.xy(1, 5));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        mainTab.add(panel3, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        nextButton = new JButton();
        nextButton.setActionCommand("nextAction");
        nextButton.setText("Следующий");
        panel3.add(nextButton, cc.xy(1, 1));
        previousButton = new JButton();
        previousButton.setActionCommand("previousAction");
        previousButton.setText("Предыдущий");
        panel3.add(previousButton, cc.xy(1, 3));
        exitButton = new JButton();
        exitButton.setActionCommand("exitAction");
        exitButton.setText("Выход");
        mainTab.add(exitButton, new GridConstraints(4, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        editCheckBox = new JCheckBox();
        editCheckBox.setActionCommand("changeEditMode");
        editCheckBox.setText("Режим редактирования статьи");
        mainTab.add(editCheckBox, new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        mainTab.add(scrollPane1, new GridConstraints(1, 0, 3, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        definitionPane = new JEditorPane();
        definitionPane.setContentType("text/html");
        definitionPane.setDragEnabled(false);
        definitionPane.setEditable(false);
        definitionPane.setMinimumSize(new Dimension(600, 400));
        definitionPane.setPreferredSize(new Dimension(800, 600));
        definitionPane.putClientProperty("JEditorPane.honorDisplayProperties", Boolean.FALSE);
        scrollPane1.setViewportView(definitionPane);
        keyword = new JFormattedTextField();
        keyword.setEditable(true);
        mainTab.add(keyword, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        settingTab = new JPanel();
        settingTab.setLayout(new GridLayoutManager(2, 3, new Insets(5, 5, 5, 5), -1, -1));
        tabbedPane1.addTab("Установки", settingTab);
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new GridLayoutManager(4, 4, new Insets(5, 5, 5, 5), -1, -1));
        settingTab.add(panel4, new GridConstraints(0, 1, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, new Dimension(400, -1), new Dimension(400, -1), null, 0, false));
        panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null));
        hostName = new JFormattedTextField();
        hostName.putClientProperty("html.disable", Boolean.TRUE);
        panel4.add(hostName, new GridConstraints(0, 1, 1, 3, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        userName = new JFormattedTextField();
        userName.putClientProperty("html.disable", Boolean.TRUE);
        panel4.add(userName, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        databaseName = new JFormattedTextField();
        databaseName.putClientProperty("html.disable", Boolean.TRUE);
        panel4.add(databaseName, new GridConstraints(1, 1, 1, 3, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("База данных");
        panel4.add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("Хост");
        panel4.add(label2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("Имя пользователя");
        panel4.add(label3, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label4 = new JLabel();
        label4.setText("Пароль");
        panel4.add(label4, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        password = new JFormattedTextField();
        password.putClientProperty("html.disable", Boolean.TRUE);
        panel4.add(password, new GridConstraints(2, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        disconnectDBButton = new JButton();
        disconnectDBButton.setActionCommand("disconnectDB");
        disconnectDBButton.setText("Отключиться");
        panel4.add(disconnectDBButton, new GridConstraints(3, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        connectDBButton = new JButton();
        connectDBButton.setActionCommand("connectDB");
        connectDBButton.setText("Подключиться");
        panel4.add(connectDBButton, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel5 = new JPanel();
        panel5.setLayout(new GridLayoutManager(3, 2, new Insets(5, 5, 5, 5), -1, -1));
        settingTab.add(panel5, new GridConstraints(1, 1, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, new Dimension(400, -1), new Dimension(400, -1), null, 0, false));
        panel5.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null));
        fileName = new JFormattedTextField();
        fileName.putClientProperty("html.disable", Boolean.TRUE);
        panel5.add(fileName, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        copyToDBButton = new JButton();
        copyToDBButton.setActionCommand("copyToDB");
        copyToDBButton.setText("Копировать в базу данных");
        panel5.add(copyToDBButton, new GridConstraints(1, 0, 2, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        fileNameSelectButton = new JButton();
        fileNameSelectButton.setActionCommand("fileNameSelect");
        fileNameSelectButton.setText("...");
        panel5.add(fileNameSelectButton, new GridConstraints(0, 1, 2, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        fileSource = new JRadioButton();
        fileSource.setActionCommand("fileSelect");
        fileSource.setLabel("Из файла");
        fileSource.setText("Из файла");
        settingTab.add(fileSource, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        databaseSource = new JRadioButton();
        databaseSource.setActionCommand("databaseSelect");
        databaseSource.setLabel("База данных");
        databaseSource.setSelected(true);
        databaseSource.setText("База данных");
        settingTab.add(databaseSource, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel6 = new JPanel();
        panel6.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        panel1.add(panel6, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(-1, 20), new Dimension(-1, 20), new Dimension(-1, 20), 0, false));
        statusLine = new JLabel();
        statusLine.setFocusable(false);
        statusLine.setText("OK");
        panel6.add(statusLine);
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(databaseSource);
        buttonGroup.add(fileSource);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}
